#coding=utf-8
#Boa:Frame:FrameRegionSales

import wx

from MyListCtrl import *
from product import *
from ctrlAider import *
from wx.lib.splitter import MultiSplitterWindow

def create(parent):
    return FrameRegionSales(parent)

[wxID_FRAMEREGIONSALES] = [wx.NewId() for _init_ctrls in range(1)]

class FrameRegionSales(wx.Frame):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Frame.__init__(self, id=wxID_FRAMEREGIONSALES,
              name=u'FrameRegionSales', parent=prnt, pos=wx.Point(626, 193),
              size=wx.Size(1140, 600), style=wx.DEFAULT_FRAME_STYLE,
              title=u'\u5730\u533a\u9500\u91cf')
        self.SetClientSize(wx.Size(1124, 580))
        self.SetBackgroundStyle(wx.BG_STYLE_SYSTEM)
        self.Center(wx.HORIZONTAL)
        self.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.BOLD, False,
              u'\u6977\u4f53_GB2312'))
        self.SetAutoLayout(False)
        self.SetBackgroundColour(wx.Colour(183, 209, 173))
        self.Bind(wx.EVT_CLOSE, self.OnFrameRegionSalesClose)

    def __init__(self, parent):
        self._init_ctrls(parent)
        
        # left:category  right:product
        self.splitterWin = MultiSplitterWindow(self, style=wx.SP_LIVE_UPDATE)
        # up:product down:purchase
        self.splitterRight = MultiSplitterWindow(self.splitterWin, style=wx.SP_LIVE_UPDATE)
        # left:planed mid:ordered right:shipped
        self.splitterPurchase = MultiSplitterWindow(self.splitterRight, style=wx.SP_LIVE_UPDATE)
        
        self.categoryList = MyCategoryTreeCtrl(self.splitterWin)

        self.proList = MyListCtrlProductRegionSale(self.splitterRight)

        
        self.lcPurDetail = MyListCtrlProPurDetail(self.splitterPurchase)
        self.lcSaleDetail = MyListCtrlProSaleDetail(self.splitterPurchase)
        
        self.splitterPurchase.AppendWindow(self.lcPurDetail, 500)        
        self.splitterPurchase.AppendWindow(self.lcSaleDetail, 500)
        self.splitterPurchase.SetOrientation(wx.HORIZONTAL)
        self.splitterPurchase.SizeWindows()
        
        #self.splitterRight.SplitHorizontally(self.proList, self.splitterPurchase, 240)
        self.splitterRight.AppendWindow(self.proList, 400)
        self.splitterRight.AppendWindow(self.splitterPurchase, 150)
        self.splitterRight.SetOrientation(wx.VERTICAL)
        self.splitterRight.SizeWindows()
        
        self.splitterWin.AppendWindow(self.categoryList, 150)
        self.splitterWin.AppendWindow(self.splitterRight, 250)
        
        self.splitterWin.Move(wx.Point(10, 30))
        # 设置大小为什么必须放在最后？放在AppendWindow之前导致sashPos参数不起作用
        self.splitterWin.SetSize(wx.Size(1190, 500))
        self.splitterWin.SetOrientation(wx.HORIZONTAL)
        self.splitterWin.SizeWindows()
        
                
        #self.splitterWin.SplitVertically(self.categoryList, self.splitterRight, 140)
        
        self.categoryList.Bind(wx.EVT_TREE_SEL_CHANGED,
                                self.OnTreeSelChanged)
        self.proList.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnProAllItemSelected)
        #self.proList.Bind(wx.EVT_LEFT_DCLICK, self.OnProListLeftDclick)
        
        #self.lcPurchasePlan.Bind(wx.EVT_LEFT_DCLICK, self.OnPlanedListLeftDclick)
        #self.lcPurchaseOrder.Bind(wx.EVT_LEFT_DCLICK, self.OnOrderedListLeftDclick)
        #self.lcPurchaseShipped.Bind(wx.EVT_LEFT_DCLICK, self.OnShippedListLeftDclick)
        
        self.curKeywords = []
        self.curCategory = None
        #self.refreshProductList()

    def OnTreeSelChanged(self, event):
        self.curCategory = self.categoryList.getSelection()
        self.refreshProductList()        
        event.Skip()
        
    def OnProAllItemSelected(self, event):

        item = self.proList.GetNextItem(-1, state = wx.LIST_STATE_SELECTED)
        proId = int(self.proList.GetItem(item, self.proList.iId).GetText())
        self.lcPurDetail.refresh({'proId':proId})
        self.lcSaleDetail.refresh({'proId':proId, 
                "regionId":self.m_regionId})

    def refreshProductList(self):

        categoryIds = []
        if (self.curCategory is not None):
            categoryId = self.curCategory.getFieldVal(index=CDataCategory.COL_ID)
            categoryIds = CDataCategory.getChildId(categoryId)
        
        keywordIds = []

        self.proList.setFilter(keywordIds, categoryIds)
        
        self.proList.refresh()
        
    def set_region(self,regionId):
        
        self.m_regionId = regionId
        self.proList.setRegion(regionId)
        self.proList.refresh()
        
        
    def OnFrameRegionSalesClose(self, event):
        self.Show(False)
